package com.ethereal.petmanagement.service.impl;

import com.ethereal.petmanagement.dao.IAdminDao;
import com.ethereal.petmanagement.dao.impl.AdminDaoImpl;
import com.ethereal.petmanagement.dto.AdminDTO;
import com.ethereal.petmanagement.dto.ResponseDTO;
import com.ethereal.petmanagement.dto.UserDTO;
import com.ethereal.petmanagement.service.IAdminService;
import com.ethereal.petmanagement.util.CodeMsg;
import com.ethereal.petmanagement.util.PasswordUtil;

import java.sql.Timestamp;

public class AdminServiceImpl implements IAdminService {
    static IAdminDao adminDao = new AdminDaoImpl();

    //管理员登录功能
    @Override
    public ResponseDTO<AdminDTO> login(AdminDTO adminDTO) {
        String inputPassword = adminDTO.getPassword(); //获取登录密码
        ResponseDTO<AdminDTO> responseDTO = adminDao.findAdminByPhone(adminDTO); //dao登录
        AdminDTO adminDTO1 = responseDTO.getData(); //获取数据库查到的数据
        if (adminDTO1.getSalt() == null){
            return ResponseDTO.errorByMsg(CodeMsg.PHONE_NOT_REGISTERED); //没有盐值，说明未注册
        }
        if (!PasswordUtil.verify(inputPassword, adminDTO1.getPassword(), adminDTO1.getSalt())){
            return ResponseDTO.errorByMsg(CodeMsg.PASSWORD_ERROR); //密码加密验证失败，密码错误
        }
        return ResponseDTO.success(adminDTO1); //登录成功
    }

    //添加管理员功能
    @Override
    public ResponseDTO<Boolean> addAdmin(AdminDTO adminDTO) {
        //验证手机号是否注册
        String salt = adminDao.findAdminByPhone(adminDTO).getData().getSalt();
        if (salt != null){
            return ResponseDTO.errorByMsg(CodeMsg.PHONE_ALREADY_EXIST); //有盐值，已注册
        }
        adminDTO.setRoleType((byte) 2);
        adminDTO.setId(PasswordUtil.generateId()); //设置id
        adminDTO.setSalt(PasswordUtil.generateSalt()); //设置盐值
        adminDTO.setCreateTime(new Timestamp(System.currentTimeMillis())); //设置创建时间
        adminDTO.setPassword(PasswordUtil.encrypt(adminDTO.getPassword(), adminDTO.getSalt())); //设置密码加密
        //获取数据库返回的信息存入responseDTO
        if (adminDao.insertAdmin(adminDTO) == 0){
            return ResponseDTO.errorByMsg(CodeMsg.REGISTER_ERROR_DATABASE);
        }
        return ResponseDTO.success(true); //错误码为0，注册成功
    }

    //修改管理员信息
    @Override
    public ResponseDTO<Boolean> updateAdmin(AdminDTO adminDTO) {
        //表单验证
        if (adminDTO.getUsername().length() < 2 || adminDTO.getUsername().length() > 6 || adminDTO.getPhone().length() != 11){
            return ResponseDTO.errorByMsg(CodeMsg.PARAMETER_FORMAT_ERROR); //参数校验不通过
        }
        //验证手机号是否注册
        String salt = adminDao.findAdminByPhone(adminDTO).getData().getSalt();
        if (salt != null){
            return ResponseDTO.errorByMsg(CodeMsg.PHONE_ALREADY_EXIST); //有盐值，已注册
        }
        //会话验证
        if (adminDTO.getId() == null){
            return ResponseDTO.errorByMsg(CodeMsg.USER_NOT_LOGIN);
        }
        adminDTO.setUpdateTime(new Timestamp(System.currentTimeMillis())); //设置更新时间
        if (adminDao.updateAdmin(adminDTO) == 0){
            return ResponseDTO.errorByMsg(CodeMsg.DATABASE_ERROR);
        }
        return ResponseDTO.success(true);
    }

    /* 删除用户
    public ResponseDTO<Boolean> deleteUser(UserDTO userDTO){
        int rows = adminDao.deleteUser(userDTO);
        if (rows == 0){
            return ResponseDTO.errorByMsg(CodeMsg.DATABASE_ERROR);
        }
        return ResponseDTO.success(true);
    }*/
}
